package com.dyb.recuitment.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyb.recuitment.entity.Admin;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.mapper.LoginMapper;
import com.dyb.recuitment.mapper.UserMapper;
import com.dyb.recuitment.service.ILoginService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-01-14
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, User> implements ILoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public User login(User user) {
        user.setStatus(1);
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        List<User> result = loginMapper.selectList(queryWrapper);
        if (result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    @Override
    public Admin loginAdmin(Admin admin) {
        admin.setStatus(1);
        admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
        List<Admin> result = loginMapper.loginAdmin(admin);
        if (result.size() > 0) {
            return result.get(0);
        }
        return null;
    }
}
